//
//  HomeViewController.m
//  YiLiao
//
//  Created by 丁超人 on 16/5/8.
//  Copyright © 2016年 丁超人. All rights reserved.
//
#define kWIDTH [UIScreen mainScreen].bounds.size.width
#import "HomeViewController.h"
#import "WeatherView.h"
#import "TPViewCell.h"
@interface HomeViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic, strong)WeatherView *weatherV;
@property(nonatomic ,strong) NSMutableArray *images;

@property(nonatomic ,strong) UICollectionView *collectionView;

@property(nonatomic ,strong) NSIndexPath *indexpath;

@property(nonatomic ,assign) NSInteger index;

@property(nonatomic ,strong) NSTimer *timer;
@end

@implementation HomeViewController
-(NSMutableArray *)images
{
    if (!_images) {
        _images = [NSMutableArray array];
        
        for (int i = 1; i < 11; i++) {
            
            NSString *path = [NSString stringWithFormat:@"%02d.jpg",i];
            
            [_images addObject:path];
            
        }
    }
    
    return _images;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.index = 1000;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(kWIDTH, 200);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 80, kWIDTH , 200) collectionViewLayout:layout];
    
    collectionView.backgroundColor = [UIColor greenColor];
    
    
    [collectionView registerClass:[TPViewCell class] forCellWithReuseIdentifier:@"CollectionViewCell"];
    
    self.collectionView = collectionView;
    
    
    // 设置分页
    collectionView.pagingEnabled = YES;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    
    
    NSIndexPath *indexpath = [NSIndexPath indexPathForItem:1000 inSection:0];
    
    self.indexpath = indexpath;
    
    [collectionView scrollToItemAtIndexPath:indexpath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    
    [self.view addSubview:collectionView];
    
    
    [self addTimer];

    self.title = @"快医";
    
    UIBarButtonItem *leftMenu = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"left"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpToMenuVC)];
    
    self.navigationItem.leftBarButtonItem = leftMenu;

    [self weatherView];
    
}
- (void)addTimer
{
    NSTimer *timer = [NSTimer timerWithTimeInterval:2 target:self selector:@selector(scollImage) userInfo:nil repeats:YES];
    
    self.timer = timer;
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)deleteTimer
{
    [self.timer invalidate];
    
    self.timer = nil;
}

-(void)scollImage
{
    
    self.index ++;
    
    NSLog(@"-------- %ld",self.index);
    
    NSIndexPath *indexpath = [NSIndexPath indexPathForItem:self.index inSection:0];
    
    [self.collectionView scrollToItemAtIndexPath:indexpath atScrollPosition:0 animated:YES];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1000*1000;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TPViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionViewCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor redColor];
    
    NSString *imageUrl = self.images[indexPath.item%10];
    
    UIImage *image = [UIImage imageNamed:imageUrl];
    
    cell.image = image;
    
    return cell;
    
}

// 开始滚动
-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    [self deleteTimer];
}


// 停止滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 页面的偏移量，如果是0表示页面没有变化
    NSInteger offset = (scrollView.contentOffset.x / scrollView.bounds.size.width);
    
    self.index = offset;
    NSLog(@"==> %zd", offset);
    
    [self addTimer];
}


// 跳转菜单
-(void)jumpToMenuVC {

    NSLog(@"点击了左侧按钮");

}

-(void)weatherView{

    WeatherView *weatherV = [[WeatherView alloc]initWithFrame:CGRectMake(0, 0, 375, 80)];
    weatherV.backgroundColor = [UIColor orangeColor];
    
    self.weatherV = weatherV;
    [self.view addSubview:weatherV];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
